<template>
	<view class="iflex column fill aic">
		<view class="iflex column order-item" @click.stop="tapToOrderDetail">
			<view class="iflex jcsb aic fill order-item__header">
				<text>订单号：{{info.orderNumber}}</text>
				<text class="order-item__header__status">
					{{dispatchStatusFormat(info.dispatchStatus)}}
				</text>
			</view>

			<view class="iflex fill order-item__desc">
				服务类型：{{info.serviceType}}
			</view>
			<view class="iflex fill order-item__desc">
				用户名字：{{info.userName}}
			</view>
			<view class="iflex fill order-item__desc">
				联系电话：{{info.contactPhone}}
			</view>
			<view class="iflex fill order-item__desc">
				下单时间：{{info.orderTime}}
			</view>
			<view class="iflex fill order-item__desc">
				备注：{{info.orderRemark}}
			</view>

			<view class="iflex jcfe fill order-item__footer">
				<u-button v-if="info.dispatchStatus === DispatchStatus.Processing" type="primary"
					@click="tapTakeOrder">接单</u-button>
				<u-button v-else-if="info.dispatchStatus === DispatchStatus.Confirmed" type="primary"
					@click="tapApplayFinish">申请完成</u-button>
			</view>
		</view>
	</view>
</template>

<script lang="ts">
	import { defineComponent } from "vue"
	export default defineComponent({
		// #ifdef MP-WEIXIN
		options: {
			virtualHost: true,
			// styleIsolation: 'shared'
		},
		// #endif
	})
</script>

<script setup lang="ts" name="OrderItem">
	import type { AnyObj } from '@/types/global';
	import { defineProps, withDefaults, ref, defineEmits } from 'vue';
	import { DispatchStatus } from '@/utils/constant';
	import { dispatchStatusFormat } from '@/utils/format';
	import { takeOrder, completedOrder } from '@/apis/trade';
	import { isResultSuccess } from '@/utils/http';
	const emit = defineEmits<{
		refresh : [info: AnyObj]
	}>();
	const props = withDefaults(defineProps<{
		info : AnyObj
	}>(), {
		info: () => ({})
	});
	function tapToOrderDetail() {
		uni.navigateTo({
			url: `/pages-order/order-detail/order-detail?orderId=${props.info.orderId}&trackingNumber=${props.info.trackingNumber}`
		})
	}

	function tapTakeOrder() {
		takeOrder(props.info.trackingNumber).then((res) => {
			if (!isResultSuccess(res, '接单成功')) return;
			setTimeout(() => {
				emit('refresh', props.info)
			}, 1500);
		});
	}

	function tapApplayFinish() {
		const { info } = props;
		completedOrder(info.trackingNumber).then((res) => {
			if (!isResultSuccess(res, '申请完成成功')) return;
			setTimeout(() => {
				emit('refresh', props.info)
			}, 1500);
		});
	}
</script>

<style lang="scss">
	.order-item {
		margin-top: 20rpx;
		padding: 20rpx;
		width: 686rpx;
		border-radius: 16rpx;
		border: 1px solid #FFF;
		background: linear-gradient(180deg, #FEEDE0 -4.43%, #FFF 12.73%);

		&__header {
			padding-bottom: 24rpx;
			color: #000000;
			font-size: 30rpx;
			font-weight: 500;
			border-bottom: 1px solid #DDE4F1;

			&__status {
				color: #3ca961;
				font-size: 28rpx;
			}
		}

		&__desc {
			margin-top: 20rpx;
			color: #4b5466;
			font-size: 28rpx;
			font-weight: 400;
		}

		&__footer {
			margin-top: 32rpx;
			padding-top: 24rpx;
			border-top: 1px solid #DDE4F1;
		}
	}
</style>